﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Text;

namespace ManageChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            OracleConnection connection = new OracleConnection("Data Source=192.168.1.15:1521/TYfood;User ID=kxt;Password=1qaz2wsx");
            OracleCommand command = new OracleCommand("select table_name from user_tables", connection);
            using (connection)
            {
                connection.Open();
                var adapter = new OracleDataAdapter(command);
                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);

                foreach (DataRow row in dataTable.Rows)
                {
                    List<string> list = new List<string>();
                    var searchCondition = new OracleCommand("SELECT Constraint_Name,Search_Condition FROM user_constraints WHERE table_name = '" + row[0] + "' and owner = 'KXT'", connection);
                    var checkerReader = searchCondition.ExecuteReader();
                    while (checkerReader.Read())
                    {
                        if (checkerReader[1] is DBNull)
                        {
                            continue;
                        }

                        if (list.Contains(checkerReader[1].ToString()))
                        {
                            //delete
                            OracleCommand deleteCommand = new OracleCommand("alter table " + row[0] + " drop constraint " + checkerReader[0], connection);
                            deleteCommand.ExecuteNonQuery();
                            Console.WriteLine(checkerReader[0] + "\tdeleted");
                        }
                        else
                        {
                            list.Add(checkerReader[1].ToString());
                        }
                    }
                }
            }

        }
    }
}
